package com.hznu.ez4life.modules.controller;


import com.hznu.ez4life.modules.entity.Orders;
import com.hznu.ez4life.modules.service.OrdersService;
import com.hznu.ez4life.myBeans.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    @PostMapping("/addOne")
    public R addOne(@RequestBody Orders orders)
    {
        try
        {
            ordersService.addOne(orders);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findOrderingByNuserId/{nuserId}")
    public R findOrderingByNuserId(@PathVariable Integer nuserId)
    {
        try
        {
            return R.ok().put("myOrderingList",ordersService.findOrderingByNuserId(nuserId));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findOrderedByNuserId/{nuserId}")
    public R findOrderedByNuserId(@PathVariable Integer nuserId)
    {
        try
        {
            return R.ok().put("myOrderedList",ordersService.findOrderedByNuserId(nuserId));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
    @GetMapping("/findOrderingByHuserId/{huserId}")
    public R findOrderingByHuserId(@PathVariable Integer huserId)
    {
        try
        {
            return R.ok().put("myOrderingList",ordersService.findOrderingByHuserId(huserId));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findOrderedByHuserId/{huserId}")
    public R findOrderedByHuserId(@PathVariable Integer huserId)
    {
        try
        {
            return R.ok().put("myOrderedList",ordersService.findOrderedByHuserId(huserId));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
    
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try
        {
            return R.ok().put("order",ordersService.findById(id));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/modifyById")
    public R modifyById(@RequestBody Orders orders)
    {
        try
        {
            ordersService.updateById(orders);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
}

